<?php
namespace app\admin\controller;

use \think\facade\Db;
use app\middleware\JWTAUTH;
use app\admin\model\accounttype;
use app\admin\model\Procount;
use app\admin\model\Proplan;
use app\admin\model\Page;
use app\admin\model\Url;
use app\admin\model\Blacklist;
use think\exception\ValidateException;

class Page
{
    protected $middleware=['JWTAUTH'];

    public function create(){
      if($this->request->action()=='post'){

          try{
            $pro_page_data=array(
                'name' => $this->request->param('name'),
                'url'  => $this->request->param('url'),
                'type' => $this->request->param('type'),
                'keys' => $this->request->param('keys'),
                'token' => $this->request->param('token'),
                'path' => $path,  //后台生成的path,页面path
                'owned_user_id' => $procount_own_user_id,
                'proplan_id' => $this->request->param('proplan_id'),
                'proaccount_id' => $this->request->param('proaccount_id'),
                'switch' => $this->request->param('switch'),
                'description' => $this->request->param('description'),
                'create_time' => time(),
                'expires_time' => $this->request->param('expires_time'),
            );

            $this->validate(app\validate\Page::class)
                  ->scene('create')
                  ->check($pro_page_data);
            $propage=new Propage;
            $propage->save($pro_page_data);

          }catch(ValidateException $e){
              throw($e);
          }
      }else{
          return json_encode(array(
              'status' => '103',
              'message' => '方法不支持，请用post',
          ));
      }
    }

    public function edit()
    {
      if($this->request->action()=='post'){

          $propage_own_user_id=$token_user_id; //从token中取出

          try{
            $id=$this->request->param('id');
            $pro_page_data=array(
              'name' => $this->request->param('name'),
              'url'  => $this->request->param('url'),
              'type' => $this->request->param('type'),
              'keys' => $this->request->param('keys'),
              'token' => $this->request->param('token'),
              'path' => $path,  //后台生成的path,页面path
              'owned_user_id' => $procount_own_user_id,
              'proplan_id' => $this->request->param('proplan_id'),
              'proaccount_id' => $this->request->param('proaccount_id'),
              'switch' => $this->request->param('switch'),
              'description' => $this->request->param('description'),
              'create_time' => time(),
              'expires_time' => $this->request->param('expires_time'),
            );

            $this->validate(app\validate\Page::class)
                  ->scene('edit')
                  ->check($pro_plan_data);
            $propage=Page::find(array('id'=>$id,'owned_user_id' => $propage_own_user_id)); //注意proaccount_id

            if(empty($propage)){
                return json_encode(array(
                    'status' => '201',
                    'message' => '没有该推广页面',
                ));
            }
            $propage->save($pro_page_data);

          }catch(ValidateException $e){
              throw($e);
          }
      }else {
          return json_encode(
            array(
              'status' => '103',
              'message' =>'方法错误',
            )
          );
      }
    }


    public function delete(){
        $propage_id=$this->request->param('id');
        $propage_own_user_id=$token_user_id; //从token中取出
        try{
            $this->validate(app\validate\Propage::class)
                ->scene('delete')
                ->check($propage_id);

            $propage=Propage::find(array(
                'id' => $propage_id,
                'owned_user_id' => $propage_own_user_id,
            ));  //注意 proaccount_id

            if($propage->delete()){
                return json_encode(array(
                    'status' => '100',
                    'message' => '删除成功',
                ));
            }else{
                return json_encode(array(
                    'status' => '203',
                    'message' => '删除失败，请联系管理员查看原因',
                ));
            }
        }catch(ValidateException $e){
            throw($e);
        }
    }

    public findbyname(){
        $propage_name=$this->request->param('name');
        $propage_own_user_id=$token_user_id; //从token中取出
        try{
            $this->validate(app\validate\Propage::class)
                ->scene('findbyname')
                ->check($propage_name);

            $propage=Page::find(array(
                'name' => $propage_name,
                'owned_user_id' => $propage_own_user_id,
            ));

            if(!empty($propage)){
                return json_encode($propage);
            }else{
                return json_encode(array(
                    'status' => '303',
                    'message' => '未查找到数据',
                ));
            }
        }catch(ValidateException $e){
            throw($e);
        }
    }
}
